package com.tzb.restorder.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.tzb.restorder.entity.Product;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * @Description 熔断降级
 * @Author tzb
 * @Date 2020/11/7 17:03
 * @Version 1.0
 **/
public class ExceptionUtils {

    //限流熔断业务逻辑
    public static SentinelClientHttpResponse handleBlock(HttpRequest request,
                                                         byte[] body,
                                                         ClientHttpRequestExecution execution,
                                                         BlockException ex) {
        Product product = new Product();
        product.setProductName("block");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }

    //异常降级业务逻辑
    public static SentinelClientHttpResponse handleFallback(HttpRequest request,
                                                            byte[] body,
                                                            ClientHttpRequestExecution execution,
                                                            BlockException ex) {
        Product product = new Product();
        product.setProductName("fallback");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }
}
